// pages/advantage/advantage.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		advantageList2: [],
		leftList2: [],
		rightList2: [],
		leftHight2: 0,
		rightHight2: 0,

		start: 0,
		page: 1,
		size: 8,
		// allNum: 0,
		isEnd: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getData(this.data.leftHight2,
			this.data.rightHight2,
			this.data.leftList2,
			this.data.rightList2,
			this.data.start);
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		//获取更多
		this.data.start = this.data.page * this.data.size;
		this.getData(this.data.leftHight2,
			this.data.rightHight2,
			this.data.leftList2,
			this.data.rightList2,
			this.data.start,
			this.data.currentTab);

		this.data.page += 1;

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//获取列表
	getData: function(lastLH, lastRH, lastLeft, lastRight, start) {
		// console.log("请求中");
		var that = this;
		var url_ = getApp().globalData.url_;
		// console.log(url_ + '/api/advantage/all?start='+ start +'&count='+ that.data.size)
		if (that.data.isEnd) {
			return
		}
		wx.request({
			url: url_ + '/api/advantage/all?start=' + start + '&count=' + that.data.size,
			method: "GET",
			dataType: "json",
			success: function(res) {
				console.log(res.data.data)

				var allData2 = res.data.data;
				if (allData2.length < that.data.size || !allData2.length) {
					that.setData({
						isEnd: true
					})
				}
				//定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
				var leftH2 = lastLH;
				var rightH2 = lastRH;
				var leftData2 = [];
				var rightData2 = [];
				var CoverWidth2 = 0;
				var CoverHeight2 = 0;

				for (let j = 0; j < allData2.length; j++) {
					var img = url_ + allData2[j].image;
					allData2[j].image = img;
					CoverWidth2 = parseInt(allData2[j].imagewidth)
					CoverHeight2 = parseInt(allData2[j].imageheight);
					var currentItemHeight2 = parseInt((CoverHeight2 * 375) / CoverWidth2);
					if (leftH2 == rightH2 || leftH2 <
						rightH2) { //判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
						leftData2.push(allData2[j]);
						leftH2 += currentItemHeight2;
					} else {
						rightData2.push(allData2[j]);
						rightH2 += currentItemHeight2;
					}
				}
				//新旧数据合并
				var lastLeftData = lastLeft;
				var lastRightData = lastRight;
				lastLeftData.push(...leftData2);
				lastRightData.push(...rightData2);
				//畅销商品
				that.setData({
					productList: res.data.data,
					leftHight2: leftH2,
					rightHight2: rightH2,
					leftList2: lastLeftData,
					rightList2: lastRightData,
				})

				that.setData({
					advantageList2: res.data.data,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
	},

	advGoDetail: function(e) {
		console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: "../advantage/advantageDetails?id=" + e.currentTarget.dataset.id
		})
	}
})
